package com.snail.generator.common;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> {
    /** 成功状态码 */
    private static final int SUCCESS_CODE = 1;
    /** 错误状态码 */
    private static final int ERROR_CODE = 0;

    private int code;
    private T data;
    private String msg;

    public Boolean isSuccess() {
        return this.code == SUCCESS_CODE;
    }


    public static <T> R<T> success() {
        return success(null);
    }

    public static <T> R<T> success(T data) {
        return success(data, null);
    }

    public static <T> R<T> success(T data, String msg) {
        return new R<>(SUCCESS_CODE, data, msg);
    }

    public static <T> R<T> error(String msg) {
        return error(null, msg);
    }

    public static <T> R<T> error(T data, String msg) {
        return new R<>(ERROR_CODE, data, msg);
    }
}
